package com.aidex.biz.controller;

import com.aidex.biz.domain.PersonInfo;
import com.aidex.biz.service.PersonInfoService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.model.LoginUser;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人员信息基础Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/biz/bizPersonInfo")
public class PersonInfoController extends BaseController {
    @Autowired
    private PersonInfoService personInfoService;

    /**
     * 查询人员信息基础列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(PersonInfo personInfo, HttpServletRequest request, HttpServletResponse response) {
        personInfo.setPage(new PageDomain(request, response));
        return R.data(personInfoService.findPage(personInfo));
    }

    /**
     * 获取人员信息基础详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:query')")
    @GetMapping(value = "/{id}")
    public R<PersonInfo> detail(@PathVariable("id") String id) {
        return R.data(personInfoService.get(id));
    }

    /**
     * 新增人员信息基础
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:add')")
    @Log(title = "人员信息基础", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated PersonInfo personInfo) {
        return R.status(personInfoService.save(personInfo));
    }

    /**
     * 修改人员信息基础
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:edit')")
    @Log(title = "人员信息基础", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated PersonInfo personInfo) {
        return R.status(personInfoService.save(personInfo));
    }


    /**
     * 删除人员信息基础
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:remove')")
    @Log(title = "人员信息基础", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(personInfoService.deletePersonInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(personInfoService.findMaxSort(new PersonInfo()));
    }

    /**
     * 导出人员信息基础列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPersonInfo:export')")
    @Log(title = "人员信息基础", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(PersonInfo personInfo) {

        List<PersonInfo> list = personInfoService.findList(personInfo);
        ExcelUtil<PersonInfo> util = new ExcelUtil<PersonInfo>(PersonInfo.class);
        return util.exportExcel(list, "人员信息基础数据");
    }

    @Log(title = "人员信息基础导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<PersonInfo> util = new ExcelUtil<PersonInfo>(PersonInfo.class);
        List<PersonInfo> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = personInfoService.importUser(userList);
        return AjaxResult.success(message);
    }


}
